function eta = update_eta( HPs, Ks, gamma, layer_connections, mid_layer_width)
nLayer = length(mid_layer_width);
eta = cell(1, nLayer);

for iLayer = 1:nLayer
    
    if iLayer == 1
        conn_a = layer_connections{1, iLayer};
        gamma_a = gamma{1, iLayer};
        v = zeros(mid_layer_width(iLayer), 1);
        
        HPt = HPs{1, iLayer};
        for i1 = 1:mid_layer_width(iLayer)
            conn_a_b = conn_a{i1};
            v2 = zeros(length(conn_a_b), 1);
            tmp = (HPt{i1} * HPt{i1}');
            tmp = (tmp + tmp')/2;
            for i2 = 1: length(conn_a_b)
                v2(i2) = sum(sum( tmp .* Ks(:, :, conn_a_b(i2)) ));
            end
            v(i1, 1) = gamma_a{i1}' * v2;
            eta{1, iLayer} = v./norm(v,2);
        end
        gamma{1, iLayer} = gamma_a;
    else
        conn_a = layer_connections{1, iLayer};
        gamma_a = gamma{1, iLayer};
        
        v = zeros(mid_layer_width(iLayer), 1);
        
        HPt0 = HPs{1, iLayer - 1};
        HPt = HPs{1, iLayer};
        for i1 = 1:mid_layer_width(iLayer)
            conn_a_b = conn_a{i1};
            v2 = zeros(length(conn_a_b), 1);
            tmp = (HPt{i1} * HPt{i1}');
            tmp = (tmp + tmp')/2;
            for i2 = 1: length(conn_a_b)
                tmp2 = HPt0{conn_a_b(i2), 1} * HPt0{conn_a_b(i2), 1}';
                tmp2 = (tmp2 + tmp2')/2;
                v2(i2) = sum(sum( tmp .* tmp2));
            end
            v(i1) = gamma_a{i1}' * v2;
        end
        eta{1, iLayer} = v./norm(v,2);
    end
end

end